package com.zc.study.java开发实战1200例.第十章_网络应用基础.网络地址解析_10_1;

import java.net.InetAddress;
import java.net.UnknownHostException;

/**
 * 获取本机ip地址
 */
public class GetAddress {
    public static void main(String[] args) {
        try {
            InetAddress inetAddress = InetAddress.getLocalHost();//创建本机的InetAddress对象
            String ip = inetAddress.getHostAddress();
            String canonicalHostName = inetAddress.getCanonicalHostName();//获取本机的域名
            String hostName = inetAddress.getHostName();//获取本机的主机名
            System.out.println("本机的ip地址是"+ip+",本机的域名是"+canonicalHostName+",本机的主机名是"+hostName);

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
    }
}
